import { createSlice } from "@reduxjs/toolkit";
import { contentObj } from "screens/edit/edit.slice";
import { RootState } from "store";
// import { AppDispatch, RootState } from "store";

interface State {
  showContentid: number;
  liveDays: number;
  browseCount: number;
  contentNum: number;
  tops: Array<{ id: number }>;
  tag: string;
  content_page: number;
}

const initialState: State = {
  showContentid: -1,
  liveDays: 0,
  browseCount: 0,
  contentNum: 0,
  tops: [],
  tag: "",
  content_page: 1,
};

export const mainSlice = createSlice({
  name: "main",
  initialState,
  reducers: {
    onSetLoginInfo(state, action) {
      const { liveDays, browseCount, contentNum, tops } = action.payload;
      state.liveDays = liveDays;
      state.browseCount = browseCount;
      state.contentNum = contentNum;
      state.tops = tops;
    },
    onShowContent(state, action) {
      state.showContentid = action.payload;
    },
    onCloseContent(state) {
      state.showContentid = -1;
    },
    onShowHome(state) {
      state.showContentid = -1;
    },
    onSetTag(state, action) {
      state.tag = action.payload;
    },

    // onSetContentPage(state,action) {
    //   state.content_page  = action.payload;
    // },

  },
});

export const mainActions = mainSlice.actions;

export const selectShowContentid = (state: RootState) =>
  state.main.showContentid;
export const selectShowContent = (state: RootState) =>
  state.edit.content.filter((item) => item.id === state.main.showContentid)[0];

export const getBlogInfo = (state: RootState) => {
  const { liveDays, browseCount, contentNum } = state.main;
  return { liveDays, browseCount, contentNum };
};

export const getTopCotent = (state: RootState): contentObj[] => {
  const { tops } = state.main,
    { content } = state.edit;

  return tops.map((t) => {
    return content.filter((c) => {
      return c.id === t.id;
    })[0];
  });
};

// export const getContentPage = (state: RootState): number => {
//   return state.main.content_page;
// };
